package com.sugar.follow.up.module.business.dal.mysql.user;

import com.sugar.follow.up.framework.common.pojo.PageResult;
import com.sugar.follow.up.framework.mybatis.core.mapper.BaseMapperX;
import com.sugar.follow.up.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.sugar.follow.up.module.business.controller.admin.user.vo.BizUserPageReqVO;
import com.sugar.follow.up.module.business.controller.app.record.vo.AppRecordPageRespVO;
import com.sugar.follow.up.module.business.controller.app.record.vo.AppRecordRespVO;
import com.sugar.follow.up.module.business.dal.dataobject.user.BizUserDO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * 用户 Mapper
 *
 * @author Ysugar
 */
@Mapper
public interface BizUserMapper extends BaseMapperX<BizUserDO> {

    default PageResult<BizUserDO> selectPage(BizUserPageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<BizUserDO>()
                .likeIfPresent(BizUserDO::getNickname, reqVO.getNickname())
                .eqIfPresent(BizUserDO::getAvatar, reqVO.getAvatar())
                .eqIfPresent(BizUserDO::getMobile, reqVO.getMobile())
                .eqIfPresent(BizUserDO::getStatus, reqVO.getStatus())
                .betweenIfPresent(BizUserDO::getCreateTime, reqVO.getCreateTime())
                .orderByDesc(BizUserDO::getId));
    }

    @Select("<script>" +
            "select" +
            " temp_tab.* " +
            "from" +
            " (" +
            "  (select " +
            "    bqr.id, 1 as `type`, bqr.title, bqr.begin_time, bqr.end_time, bqr.duration_hour, " +
            "    su.nickname AS admin_user_name, bup.name AS patient_name " +
            "   from business_questionnaire_record bqr " +
            "   left join system_users su ON (su.id = bqr.admin_user_id AND su.deleted = 0) " +
            "   left join business_user_patient bup ON (bup.id = bqr.patient_id AND bup.deleted = 0) " +
            "   where bqr.deleted = 0 and bqr.is_done = 0 and bqr.user_id = #{param2} and bqr.begin_time &lt;= CURRENT_TIMESTAMP() and bqr.end_time &gt;= CURRENT_TIMESTAMP() " +
            "   order by bqr.id desc) " +
            "  union all  " +
            "  (select " +
            "    bfur.id, 2 as `type`, bfur.title, bfur.begin_time, bfur.end_time, bfur.duration_hour, " +
            "    su.nickname AS admin_user_name, bup.name AS patient_name " +
            "   from business_follow_up_record bfur " +
            "   left join system_users su ON (su.id = bfur.admin_user_id AND su.deleted = 0) " +
            "   left join business_user_patient bup ON (bup.id = bfur.patient_id AND bup.deleted = 0) " +
            "   where bfur.deleted = 0 and bfur.is_done = 0 and bfur.user_id = #{param2} and bfur.begin_time &lt;= CURRENT_TIMESTAMP() and bfur.end_time &gt;= CURRENT_TIMESTAMP() " +
            "   order by bfur.id desc)" +
            " ) temp_tab " +
            "<if test=\"param1 != null\">limit #{param1}</if>" +
            "</script>")
    List<AppRecordRespVO> getRecordAll(Integer count, Long userId);

    @Select("select " +
            " ((select count(1) FROM business_follow_up_record bfur WHERE bfur.deleted = 0 and bfur.is_done = 1 and bfur.user_id = #{param1}) + " +
            " (select count(1) FROM business_questionnaire_record bqr WHERE bqr.deleted = 0 and bqr.is_done = 1 and bqr.user_id = #{param1}))")
    Long getRecordPageCount(Long userId);

    @Select("select" +
            " temp_tab.* " +
            "from " +
            " ( " +
            "  (select bfur.id, 2 as `type`, bfur.title, bfur.done_time FROM business_follow_up_record bfur WHERE bfur.deleted = 0 and bfur.is_done = 1 and bfur.user_id = #{param1}) " +
            "  union all  " +
            "  (select bqr.id, 1 as `type`, bqr.title, bqr.done_time FROM business_questionnaire_record bqr WHERE bqr.deleted = 0 and bqr.is_done = 1 and bqr.user_id = #{param1}) " +
            " ) temp_tab " +
            "order by temp_tab.done_time desc " +
            "limit #{param2}, #{param3}")
    List<AppRecordPageRespVO> getRecordPage(Long userId, Integer skipCount, Integer pageSize);
}